// pages/shopcar/shopcar.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHide: true,
    isEmptyShow: false,
    isShowDele: 'true',
    isVisit: false,
    payOrDel: '结算',
    allIsCheck: true,
    allPrice: 9999999,
    isBindPhone:false,
    shopList: [],
    datanum:1,
    inventory:0         //库存
  },
  // 编辑
  edictDele: function () {
    if (this.data.isShowDele) {
      this.setData({
        isShowDele: false,
        isVisit: true,
        isShow: false,
        payOrDel: '删除',
      })
    } else {
      this.setData({
        isShowDele: true,
        isVisit: false,
        isShow: true,
        payOrDel: '结算'
      })
    }

  },
  // 跳转详情
  toDeatil: function (e) {
    var index = e.currentTarget.dataset.index;
    var id = this.data.shopList[index].goodsid;
    wx.navigateTo({
      url: '/pages/detail/detail?goodsid=' + id,
    })
  },
  // 结算或者全部删除 按钮
  PayOrDele: function () {
    var self = this;
    if (this.data.isShowDele) {
      // 结算
      self.payShop();
    } else {
      // 删除
      self.seleDele();
    }
  },
  //结算
  payShop: function () {
    var carid = new Array();
    for (var i = 0; i < this.data.shopList.length; i++) {
      if (this.data.shopList[i].checked) {
        carid.push(this.data.shopList[i].carid);
      }
    }
      wx.setStorage({                                   //设置本地缓存
        key: app.globalData.CARID_KEY,
        data: carid
      })
      var type = 'formShopCar'
      wx.navigateTo({
        url: '/pages/payorder/payorder?type=' + type,
      })
  },
  // 批量删除
  seleDele:function(){
    var self = this
    wx.showLoading({
      title: '正在删除...',
    })
  
    var carid = new Array();
    var list = this.data.shopList;
    for (var i = 0; i < this.data.shopList.length; i++) {
      if (this.data.shopList[i].checked) {
          carid.push(this.data.shopList[i].carid);
      }
    }
    var url = app.serverUrl + 'delCar';
    wx.request({
      url: url,
      method: 'post',
      data: {
        carid: carid.toString()
      },
      success: function (res) {
        wx.request({
          url: app.serverUrl + 'carList',      //请求的地址
          method: 'GET',
          data: {
            openid: app.openid
          },                                    //GET请求
          success: function (res) {                                  //请求成功方法

            console.log(res)

            var price = 0;
            //求总价格
            for (var i = 0; i < res.data.data.length; i++) {
              price += res.data.data[i].goodsmoney * res.data.data[i].goodsnumber;
            }
            if (res.data.data.cars.length == 0 || res.data.data.cars == null) {
              self.setData({
                isHide: true,
                isEmptyShow: false
              })
            } else {
              self.setData({
                isHide: false,
                isEmptyShow: true,
                shopList: res.data.data.cars,
                allPrice: price.toFixed(2)
              })
            }
            wx.hideLoading();
          }
        })
      }
    })
  },
  // 去逛逛
  goShop: function () {
    // console.log(1);
    wx.switchTab({
      url: '/pages/serve/serve',
    })
  },
  // //单独删除某个商品
  // signDele: function (e) {
  //   var self = this;
  //   var index = e.currentTarget.dataset.index;
  //   var carid = this.data.shopList[index].carid;
  //   var url = app.serverUrl + 'delCar';
  //   wx.request({
  //     url: url,
  //     method: 'post',
  //     data: {
  //       carid: carid
  //     },
  //     success: function (res) {
  //       self.data.shopList.splice(index, 1);
  //       var list = self.data.shopList;
  //       self.setData({
  //         shopList: list
  //       })
  //     }

  //   })
  // },
  //加
  jia:function(e){
    var self=this;
    var index = e.currentTarget.dataset.index;
    var list = self.data.shopList;
    console.log(list[index].inventory)
    var datanums = list[index].goodsnumber + 1;
    if (datanums > list[index].inventory) {
      wx.showToast({
        title: '库存不够！',
      })
      return;
    }
    list[index].goodsnumber = datanums;

    //合计价格

    var price = 0;


    var url = app.serverUrl + 'jiaCar'
    wx.request({
      url: url,
      method: 'post',
      data: {
        carid: list[index].carid,
      },
      success: function (res) {
        
        if (res.data.flag == app.suc) {

          //合计价格
          if (list[index].checked) {
            price = list[index].goodsmoney * datanums;
          }
          console.log(price);
          //求总价格
          for (var i = 0; i < list.length; i++) {
            if (i != index && list[i].checked) {
              price += list[i].goodsmoney * list[i].goodsnumber;
            }
          }

          self.setData({
            shopList: list,
            allPrice: price.toFixed(2)
          })
        } else {
          wx.showToast({
            title: res.data.message,
          })
        }
      }
    })
  },
  // 减
  jian: function (e) {
    var self=this;
    var index = e.currentTarget.dataset.index;
    var list = this.data.shopList;
    var datanums = list[index].goodsnumber - 1;
    if (datanums < 1) {
      datanums = 1;
    } 

    list[index].goodsnumber = datanums;
    var price =0;

    



    var url = app.serverUrl + 'jianCar'
    wx.request({
      url: url,
      method: 'post',
      data: {
        carid: list[index].carid,
      },
      success:function(res){
        console.log(res);
        if (res.data.flag == app.suc){


          //合计价格
          if (list[index].checked) {
            price = list[index].goodsmoney * datanums;
          }

          //求总价格
          for (var i = 0; i < list.length; i++) {
            if (i != index && list[i].checked) {
              price += list[i].goodsmoney * list[i].goodsnumber;
            }
          }

          self.setData({
            shopList: list,
            allPrice: price.toFixed(2)
          })
        }else{
          wx.showToast({
            title: '不可减少！',
          })
        }
      }
    })
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.setNavigationBarTitle({
      title: '购物车'
    })
    

  },

  //每见商品的单独选择
  checkboxChange: function (e) {

    var index = e.currentTarget.dataset.index;
    if (this.data.shopList[index].checked) {
      this.data.shopList[index].checked = false;
    } else {
      this.data.shopList[index].checked = true;
    }
    var price=0;
    var checkNum=0;
    for (var i = 0; i < this.data.shopList.length;i++){
      if (this.data.shopList[i].checked){
        price += this.data.shopList[i].goodsmoney * this.data.shopList[i].goodsnumber;
        checkNum++;
      }
    }
    this.setData({
      allPrice: price.toFixed(2)
    })
    if (checkNum == this.data.shopList.length){
      this.setData({
        allIsCheck: true
      })
    }else{
      this.setData({
        allIsCheck: false
      })
    }
  },
  //全选
  allCheck: function () {
    var self = this;
    if (this.data.allIsCheck) {


      //所有选择设置为不选择
      for (var i = 0; i < this.data.shopList.length; i++) {
        this.data.shopList[i].checked = false;
      }
      self.setData({
        allIsCheck: false,
        shopList: this.data.shopList,
        allPrice: 0
      })
    } else {
      var price=0;
      //所有选择设置为选择
      for (var i = 0; i < this.data.shopList.length; i++) {
        this.data.shopList[i].checked = true;
        price += this.data.shopList[i].goodsmoney * this.data.shopList[i].goodsnumber;
      }
      self.setData({
        shopList: this.data.shopList,
        allIsCheck: true,
        allPrice: price
      })
    }
  },

  // 绑定手机号
  btnBind: function () {
    this.bindPhone.showBind();
  },










  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var self = this
    wx.showLoading({
      title: '正在加载',
    })
    // this.bindPhone = this.selectComponent("#bindPhone");
    
    wx.request({
      url: app.serverUrl + 'carList',      //请求的地址
      method: 'GET',
      data: {
        openid: app.openid
      },                                    //GET请求
      success: function (res) {
        console.log(res)
        var price = 0;
        //求总价格
        for (var i = 0; i < res.data.data.cars.length; i++) {
          price += (res.data.data.cars[i].goodsmoney * res.data.data.cars[i].goodsnumber);
        }
        // app.phone = res.data.data.tel;
        // if (app.phone == '') {
        //   self.setData({
        //     isBindPhone: false
        //   })
        // } else {
        //   self.setData({
        //     isBindPhone: true
        //   })
        // }
        if (res.data.data.cars.length == 0 || res.data.data.cars == null) {
          self.setData({
            isHide: true,
            isEmptyShow: false
          })
        } else {
          self.setData({
            isHide: false,
            isEmptyShow: true,
            shopList: res.data.data.cars,
            allPrice: price.toFixed(2),
            allIsCheck:true
          })
        }
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self = this
    wx.request({
      url: app.serverUrl + 'carList',      //请求的地址
      method: 'GET',
      data: {
        openid: app.openid
      },                                    //GET请求
      success: function (res) {
        var price = 0;
        //求总价格
        for (var i = 0; i < res.data.data.cars.length; i++) {
          price += (res.data.data.cars[i].goodsmoney * res.data.data.cars[i].goodsnumber);
        }
        app.phone = res.data.data.tel;
        if (app.phone == '') {
          self.setData({
            isBindPhone: false
          })
        } else {
          self.setData({
            isBindPhone: true
          })
        }
        if (res.data.data.cars.length == 0 || res.data.data.cars == null) {
          self.setData({
            isHide: true,
            isEmptyShow: false
          })
        } else {
          self.setData({
            isHide: false,
            isEmptyShow: true,
            shopList: res.data.data.cars,
            allPrice: price.toFixed(2)
          })
        }
        wx.stopPullDownRefresh()
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: '/pages/index/index'
    }
  }
})